<?php

namespace app\controller;

use app\general\utils\ConversionUtils;
use app\model\BasicLanguage;
use Carbon\Carbon;
use support\Db;
use support\Request;
use Webman\Captcha\CaptchaBuilder;
use yzh52521\hash\Hash;


class LanguageController extends BaseController
{

    public function index(Request $request)
    {
        $WebUsers = Db::table('basic_language')
            ->when($request->input('keyword'), function ($query) use ($request) {
                return $query->where('name', 'like', '%'.$request->input('keyword').'%');
            })
            ->paginate($request->get('pageSize'));
        $items = $WebUsers->items();
        foreach ($items as $key=>$val){
            $items[$key] = ConversionUtils::underscoreToHump($val) ;
            $items[$key]['createTime'] = Carbon::parse($items[$key]['createTime'])->toIso8601String();
        }

        return  json($this->success('操作成功',$items,$WebUsers->total()));
    }

    /**
     * 创建
     * @param Request $request
     * @return \support\Response
     */
    public function store(Request $request)
    {
        $BasicLanguage = new BasicLanguage();
        $BasicLanguage->setRawAttributes(ConversionUtils::humpToUnderscore($request->all()));
        $BasicLanguage->version=0;
        $BasicLanguage->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 更新
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function update(Request $request,$id)
    {
        $BasicLanguage = BasicLanguage::find($id);
        if(empty($BasicLanguage)){
            return  json($this->success('操作失败'));
        }
        $data = $request->all();
        $data = ConversionUtils::humpToUnderscore($data);
        unset($data['create_time'],$data['updateTime']);
        $BasicLanguage->setRawAttributes($data);
        $BasicLanguage->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 删除
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function destroy(Request $request, $id)
    {
        BasicLanguage::destroy($id) ;
        return  json($this->success('操作成功'));
    }

    public function getAll(Request $request)
    {
        $items = Db::table('basic_country')->get();
        if(empty($items)){
            return  json($this->success('操作成功',[]));
        }
        $items = $items->toArray();

        foreach ($items as $key=>$val){
            $items[$key] = ConversionUtils::underscoreToHump($val) ;
            $items[$key]['createTime'] = Carbon::parse($items[$key]['createTime'])->toIso8601String();
        }

        return  json($this->success('操作成功',$items));
    }

}
